import java.util.ArrayList;
import jpcap.*;

/**
 * Project: SecuritySnort
 * This class will take care of determining which command
 * the user wants executed via string input and executes the
 * appropriate methods.
 *
 * @author Jonathan Buttner
 * @version Jan 21, 2010
 */
public class CommandId
{
	/**************************************************************************
	 * 							Globals
	 *************************************************************************/
	//Commands:
	private DeviceCmd _devCmd;
	private CaptureCmd _capCmd;
	private SavePackCmd _saveCmd;
	private StopLisCmd _stopLisCmd;
	private HelpCmd _helpCmd;
	private ConInfoCmd _conInfoCmd;
	private IPSaver _tallyIP;
	private TallyIPCmd _tallyCmd;
	private ForceIPCmd _forceCmd;
	//private PoisonCmd _poisonCmd;
	
	private NetworkInterface _netFace;
	
	/**************************************************************************
	 * 							Constructors
	 *************************************************************************/
	/**
	 * This is the default constructor that sets up the
	 * Command Identifier object.
	 */
	public CommandId()
	{
		_tallyIP = new IPSaver();
		_tallyCmd = new TallyIPCmd( _tallyIP );
		_devCmd = new DeviceCmd();
		//This is kind of pointless since it's going to be null anyway...
		_conInfoCmd = new ConInfoCmd( _devCmd.getPreDiv() );
		_forceCmd = new ForceIPCmd( _devCmd.getPreDiv() );
		_capCmd = new CaptureCmd( _tallyIP );
		_saveCmd = new SavePackCmd( _capCmd );
		_stopLisCmd = new StopLisCmd( _capCmd );
		_helpCmd = new HelpCmd();
		_netFace = null;
	}
	
	/**************************************************************************
	 * 							Public Methods
	 *************************************************************************/
	/**
	 * This method will determine the correct command to run based on
	 * the user's input.
	 * 
	 * @param cmdLine is the actual string the user inputed.
	 * 
	 * @return a boolean representing whether or not the
	 * user wants to continue to enter commands (exit means false).
	 */
	public boolean deterCmd( String cmdLine )
	{
		if ( cmdLine.isEmpty() )
		{
			System.out.println( "Please enter a valid command." );
			return true;
		}
		
		ArrayList < String > tokenized =
			LineParser.getArrayStr( cmdLine, " " );
		
		if ( tokenized.get(0).equalsIgnoreCase( _devCmd.getName() ) )
		{
			_devCmd.exeCommand( tokenized );
			_netFace = _devCmd.getPreDiv();
			//_conInfoCmd.setDev( _netFace );
			_updateNetFace();
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( _capCmd.getName() ) )
		{
			if ( _netFace == null ) //User has not chosen a device yet...
			{
				_devCmd.exeCommand( tokenized ); //param not really needed here
				_netFace = _devCmd.getPreDiv(); //get the device
				//_conInfoCmd.setDev( _netFace );
				//_forceCmd.setDev( _netFace );
				_updateNetFace();
			}
			
			//_capCmd.setDevice( _netFace ); //what device should I listen to?
			//_conInfoCmd.setDev( _netFace );
			//_forceCmd.setDev( _netFace );
			_capCmd.exeCommand( tokenized );
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( _saveCmd.getName() ) )
		{
			_saveCmd.exeCommand( tokenized );
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( _stopLisCmd.getName() ) )
		{
			_stopLisCmd.exeCommand( tokenized );
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( _conInfoCmd.getName() ) )
		{
			_conInfoCmd.exeCommand( tokenized );
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( _tallyCmd.getName() ) )
		{
			_tallyCmd.exeCommand( tokenized );
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( _forceCmd.getName() ) )
		{
			if ( _netFace == null ) //User has not chosen a device yet...
			{
				_devCmd.exeCommand( tokenized ); //param not really needed here
				_netFace = _devCmd.getPreDiv(); //get the device
				//_conInfoCmd.setDev( _netFace );
				//_forceCmd.setDev( _netFace );
				_updateNetFace();
			}
			_forceCmd.exeCommand( tokenized );
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( _helpCmd.getName() ) ) 
		{
			_helpCmd.exeCommand( tokenized );
		}
		else if ( tokenized.get( 0 ).equalsIgnoreCase( "exit" ) )
		{
			_cleanUp();
			return false;
		}
		else
		{
			System.out.println( "Please enter a valid command." );
			return true;
		}
		
		return true;
	}
	
	/**************************************************************************
	 * 							Private Methods
	 *************************************************************************/
	/**
	 * This method tells the necessary objects that a device was chosen.
	 */
	private void _updateNetFace()
	{
		_conInfoCmd.setDev( _netFace );
		_capCmd.setDevice( _netFace );
		_forceCmd.setDev( _netFace );
	}
	/**
	 * This method will clean up anything that needs to be done before
	 * the program exits.
	 */	
	private void _cleanUp()
	{
		_capCmd.exeStop();
		System.out.println( "\nProgram exiting..." );
	}
}
